package com.example.mobile2021606143cwj0513homework;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.mobile2021606143cwj0513homework.fragment.IndexFragment;
import com.example.mobile2021606143cwj0513homework.fragment.EnviFragment;
import com.example.mobile2021606143cwj0513homework.fragment.ETCFragment;
import com.example.mobile2021606143cwj0513homework.fragment.LightFragment;
import com.google.android.material.navigation.NavigationView;


public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private NavigationView navigation;
    private FrameLayout framelayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout, IndexFragment.newInstance("",""),"")
                .commit();
       toolbar.setTitle("智能交通系统");
       navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
           @Override
           public boolean onNavigationItemSelected(@NonNull  MenuItem item) {
               item.setCheckable(true);
               switch (item.getItemId()){
                   case R.id.itemIndex:
                       getSupportFragmentManager().beginTransaction()
                               .replace(R.id.framelayout,IndexFragment.newInstance("",""),"").commit();
                       toolbar.setTitle("智能交通系统");
                       break;
                   case R.id.itemETC:
                       getSupportFragmentManager().beginTransaction()
                               .replace(R.id.framelayout,ETCFragment.newInstance("",""),"").commit();
                       toolbar.setTitle("ETC账户");
                       break;
                   case R.id.itemEnvi:
                       getSupportFragmentManager().beginTransaction()
                               .replace(R.id.framelayout,EnviFragment.newInstance("",""),"").commit();
                       toolbar.setTitle("环境指标");
                       break;
                   case R.id.itemLight:
                       getSupportFragmentManager().beginTransaction()
                               .replace(R.id.framelayout,LightFragment.newInstance("",""),"").commit();
                       toolbar.setTitle("交通灯");
                       break;
                   case R.id.itemExit:
                       MainActivity.this.finish();
                       break;
               }
               drawerLayout.closeDrawers();
               return false;
           }
       });



    }

    private void initView() {
        toolbar =  (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
        framelayout = (FrameLayout) findViewById(R.id.framelayout);
        navigation = (NavigationView) findViewById(R.id.navigation);

    }
}